#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include "usbhost.h"


#ifdef __ANDROID__
#include <assert.h>
#include <pthread.h>
#include <unistd.h>
#include <android/log.h>
#endif

#include "pd.h"
#include <jni.h>
static JavaVM * g_jvm;
std::string allDevices = "";
#define ZKUSBTEST_JNIFUNC(name)  Java_com_zkteco_biometric_USBTest_##name

#ifdef __cplusplus
extern "C"
{
#endif

int myusb_device_added_cb(const char *dev_name, void *client_data)
{
    ZKUSBTEST_LOGD("device[%s] added\r\n", dev_name);
    allDevices.append(dev_name);
    allDevices.append("\r\n");
    return 0;
}

int myusb_device_removed_cb(const char *dev_name, void *client_data)
{
    ZKUSBTEST_LOGD("device[%s] removed\r\n", dev_name);
    return 0;
}

int myusb_discovery_done_cb(void *client_data)
{
    ZKUSBTEST_LOGD("finshed!\r\n");
    return 1;
}

void* threadRunning(void* lParam)
{
    struct usb_host_context* context = usb_host_init();
    usb_host_run(context, myusb_device_added_cb, myusb_device_removed_cb, myusb_discovery_done_cb, NULL);
}

jlong ZKUSBTEST_JNIFUNC(test)(JNIEnv *env, jclass obj)
{
    pthread_t thread_test;
    allDevices="";
    pthread_create(&thread_test, NULL, threadRunning, NULL);
    return 0;
}

jstring ZKUSBTEST_JNIFUNC(getalldevice)(JNIEnv *env, jclass obj)
{
    env->NewStringUTF(allDevices.c_str());
}


#ifdef __cplusplus
};
#endif

